package com.cn.algorithm.two.line.test;

public class BinarySearchJ {
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9};
        int index = search(arr, 55);
        System.out.println(index);
    }
    public static int search(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int start = 0;
        int end = arr.length - 1;
        int mid = start + (end - start) / 2;
        while (start <= end) {
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
            mid = start + (end - start) / 2;
        }
        return -1;
    }
}
